{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# OAI 4G Core\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 🛠️ Pre-Requisites\n",
    "\n",
    "Basically you need:\n",
    "1. Laptop/Desktop/Server for OAI EPC and OAI eNB\n",
    "   -  Ubuntu 18.04 or 20.04 Baremetal;\n",
    "   -  CPU: 4 cores x86_64 @ 3.5 GHz.\n",
    "   -  RAM: 8 GB\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Introduction\n",
    "\n",
    "General Instructions\n",
    "\n",
    "In this task you will install and configure the Evolved Packet Core (EPC), which is the core of 4G network of LTE. There are two different versions\n",
    "of the OAI EPC:\n",
    "\n",
    " 1. The most updated version of EPC is available at github openair-epc-fed ;\n",
    " 2. This version is used for simulation purposes, but it works very well with 4G RAN.\n",
    "\n",
    "We will explore both installation, however we recommend you to use the most updated one.\n",
    "\n",
    "✅ - This tutorial is made from informations from official gitlab pages:\n",
    "\n",
    "🔍 - [ develop · oai / openairinterface5G · GitLab (eurecom.fr)](https://gitlab.eurecom.fr/oai/openairinterface5g/-/blob/develop/ci-scripts/yaml_files/4g_rfsimulator_fdd_05MHz/README.md)\n",
    "\n",
    "\n",
    "🔍 - [ master · OPENAIRINTERFACE openair-epc-fed · GitHub](https://github.com/OPENAIRINTERFACE/openair-epc-fed/blob/master/docs/DEPLOY_HOME_MAGMA_MME.md)\n",
    "\n",
    "\n",
    "At the end of a set of three hands-on (this is the first one), the goal is to run the simulated OAI scenario, which consists of the 4G core (EPC), eNB and user equipment (UE), according to the image below:\n",
    "\n",
    "![4G_EPC_00](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_deploy.png)\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Install dependencies\n",
    "\n",
    "```bash\n",
    "sudo apt-get update && apt-get install -y git curl python3-pip nfs-common cmake make build-essential software-properties-common ca-certificates gnupg net-tools\n",
    "```\n",
    "\n",
    "\n",
    "- Enable packet forwarding:\n",
    "\n",
    "```bash\n",
    "sudo sysctl net.ipv4.conf.all.forwarding=1\n",
    "sudo iptables -P FORWARD ACCEPT\n",
    "```\n",
    "\n",
    "\n",
    "- Install docker and docker-compose\n",
    "\n",
    "```bash\n",
    "sudo apt-get update -y\n",
    "sudo install -m 0755 -d /etc/apt/keyrings\n",
    "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg\n",
    "sudo chmod a+r /etc/apt/keyrings/docker.gpg\n",
    "\n",
    "echo \\\n",
    "\"deb [arch=\"$(dpkg --print-architecture)\" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \\\n",
    "\"$(. /etc/os-release && echo \"$VERSION_CODENAME\")\" stable\" | \\\n",
    "sudo tee /etc/apt/sources.list.d/docker.list > /dev/null\n",
    "\n",
    "sudo apt-get update -y\n",
    "sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y\n",
    "```\n",
    "\n",
    "\n",
    "- Install USRP Hardware Driver (UHD™) Software\n",
    "\n",
    "```bash\n",
    "sudo apt-get install ccache -y\n",
    "sudo add-apt-repository ppa:ettusresearch/uhd\n",
    "sudo apt-get install libuhd-dev uhd-host -y\n",
    "\n",
    "```\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 🚀 RFSim4G EPC (RECOMMENDED)\n",
    "\n",
    "### - Install and Build EPC\n",
    "\n",
    "🌱 - Currently the images are hosted under the team account `oaisoftwarealliance`. They were previously hosted under the user account `rdefosseoai`.\n",
    "Once again you may need to log on docker-hub if your organization has reached pulling limit as anonymous.\n",
    "\n",
    "⚠️ **Attention:** It is worth mentioning that this deployment is carried out via Docker Compose and 25 PRB's, if you wish to increase the number of PRB's follow the following instructions.\n",
    "\n",
    "```bash\n",
    "docker login\n",
    "Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.\n",
    "Username:\n",
    "Password:\n",
    "```\n",
    "\n",
    "- Now Pull images:\n",
    "\n",
    "```bash\n",
    "docker pull cassandra:2.1\n",
    "docker pull redis:6.0.5\n",
    "docker pull oaisoftwarealliance/oai-hss:latest\n",
    "docker pull oaisoftwarealliance/magma-mme:latest\n",
    "docker pull oaisoftwarealliance/oai-spgwc:latest\n",
    "docker pull oaisoftwarealliance/oai-spgwu-tiny:latest\n",
    "docker pull oaisoftwarealliance/oai-enb:develop\n",
    "docker pull oaisoftwarealliance/oai-lte-ue:develop\n",
    "```\n",
    "\n",
    "🚩 - If the redis tag is not available, pick the newest available 6.0.x tag at [Docker Hub Redis Tags](https://hub.docker.com/_/redis?tab=tags).\n",
    "And re-tag them for tutorials docker-compose file to work.\n",
    "\n",
    "- Re-tag them:\n",
    "\n",
    "```bash\n",
    "docker image tag oaisoftwarealliance/oai-spgwc:latest oai-spgwc:latest\n",
    "docker image tag oaisoftwarealliance/oai-hss:latest oai-hss:latest\n",
    "docker image tag oaisoftwarealliance/oai-spgwu-tiny:latest oai-spgwu-tiny:latest\n",
    "docker image tag oaisoftwarealliance/magma-mme:latest magma-mme:latest\n",
    "```\n",
    "\n",
    "- Cloning repository openairinterface5g\n",
    "\n",
    "```bash\n",
    "git clone https://gitlab.eurecom.fr/oai/openairinterface5g.git\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Install eNB and UE dependencies\n",
    "\n",
    "```bash\n",
    "cd openairinterface5g/\n",
    "git checkout develop\n",
    "source oaienv\n",
    "cd cmake_targets\n",
    "sudo ./build_oai -I\n",
    "sudo ./build_oai -I --install-optional-packages\n",
    "sudo ./build_oai -w USRP --eNB --UE -C --build-lib \"telnetsrv enbscope uescope\"\n",
    "```\n",
    "\n",
    "---\n",
    "\n",
    "## Deploy OAI CN4G container\n",
    "\n",
    "🚩 - Just `docker-compose up -d` WILL NOT WORK!\n",
    "All the following commands SHALL be run from the folder:\n",
    "\n",
    "`ci-scripts/yaml_files/4g_rfsimulator_fdd_05MHz`\n",
    "\n",
    "- Deploy and Configure Cassandra Database\n",
    "\n",
    "```bash\n",
    "cd ../ci-scripts/yaml_files/4g_rfsimulator_fdd_05MHz\n",
    "docker compose up -d db_init\n",
    "```\n",
    "\n",
    "- Make sure everything is ok!\n",
    "\n",
    "```bash\n",
    "docker ps -a\n",
    "```\n",
    "\n",
    "![4G_EPC_01](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_01.png)\n",
    "\n",
    "\n",
    "🚩 - The next commands can only be used when you got the \"OK\" message by using the command below:\n",
    "\n",
    "`docker logs rfsim4g-db-init --follow`\n",
    "\n",
    "![4G_EPC_02](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_02.png)\n",
    "\n",
    "- Remove once it is not need anymore:\n",
    "\n",
    "```bash\n",
    "docker rm rfsim4g-db-init\n",
    "```\n",
    "\n",
    "- Deploy Magma-MME\n",
    "\n",
    "```bash\n",
    "docker compose up -d magma_mme oai_spgwu trf_gen\n",
    "```\n",
    "\n",
    "![4G_EPC_03](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_03.png)\n",
    "\n",
    "🚩 - You shall wait until all containers are healthy. About 10 seconds!\n",
    "\n",
    "```bash\n",
    "docker compose ps -a\n",
    "```\n",
    "\n",
    "![4G_EPC_04](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_04.png)\n",
    "\n",
    "- Get EPC logs\n",
    "\n",
    "🚩 - make sure the NAME of the pod is correct, use:  `docker ps -a`\n",
    "\n",
    "```bash\n",
    "docker exec -it rfsim4g-magma-mme /bin/bash -c \"tail -f /var/log/mme.log\"\n",
    "```\n",
    "\n",
    "![4G_EPC_05](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_05.png)\n",
    "\n",
    "---\n",
    "\n",
    "##  Deploy OAI eNB in RF simulator mode\n",
    "\n",
    "```bash\n",
    "docker compose up -d oai_enb0\n",
    "```\n",
    "\n",
    "![4G_EPC_07](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_07.png)\n",
    "\n",
    "Again wait for the healthy state:\n",
    "\n",
    "```bash\n",
    "docker compose ps -a\n",
    "```\n",
    "\n",
    "![4G_EPC_08](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_08.png)\n",
    "\n",
    "Check if the eNB connected to MME: with MAGMA-MME, the logs are not pushed to `stdout` but to a file at `/var/log/mme.log`\n",
    "\n",
    "```bash\n",
    "docker exec -it rfsim4g-magma-mme /bin/bash -c \"tail -f /var/log/mme.log\"\n",
    "```\n",
    "\n",
    "![4G_EPC_09](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_09.png)\n",
    "\n",
    "---\n",
    "\n",
    "## Deploy OAI LTE UE in RF simulator mode\n",
    "\n",
    "```bash\n",
    "docker compose up -d oai_ue0\n",
    "```\n",
    "\n",
    "![4G_EPC_10](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_10.png)\n",
    "\n",
    "Again a bit of patience:\n",
    "\n",
    "```bash\n",
    "docker compose ps -a\n",
    "```\n",
    "\n",
    "![4G_EPC_11](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_11.png)\n",
    "\n",
    "Making sure the OAI UE is connected:\n",
    "\n",
    "```bash\n",
    "docker logs rfsim4g-oai-enb -f\n",
    "```\n",
    "\n",
    "![4G_EPC_12](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_12.png)\n",
    "\n",
    "On the MME: be patient, the statistics display update is slow!\n",
    "\n",
    "```bash\n",
    "docker exec -it rfsim4g-magma-mme /bin/bash -c \"tail -f /var/log/mme.log\"\n",
    "```\n",
    "\n",
    "![4G_EPC_13](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_13.png)\n",
    "\n",
    "On the LTE UE:\n",
    "\n",
    "```bash\n",
    "docker exec rfsim4g-oai-lte-ue0 /bin/bash -c \"ifconfig\"\n",
    "```\n",
    "\n",
    "![4G_EPC_14](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_14.png)\n",
    "\n",
    "The tunnel `oaitun_ue1` SHALL be mounted and with an IP address in the `12.0.0.xxx` range.\n",
    "\n",
    "---\n",
    "\n",
    "## Check traffic\n",
    "\n",
    "```bash\n",
    "docker exec rfsim4g-oai-lte-ue0 /bin/bash -c \"ping -c 2 www.google.com\"\n",
    "```\n",
    "![4G_EPC_14](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_15.png)\n",
    "\n",
    "The 1st ping command is NOT using the OAI stack. My network infrastructure has a response of `13 ms` to reach this website.\n",
    "\n",
    "The 2nd ping command is using the OAI stack. So the stack takes `26.6 - 12.9 = 13.7 ms`.\n",
    "\n",
    "---\n",
    "\n",
    "## Un-deployment\n",
    "\n",
    "```bash\n",
    "docker-compose down\n",
    "```\n",
    "\n",
    "![4G_EPC_06](../FIGS/H04_4G_EPC_UNI_III/4G_EPC_06.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.7 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.7"
  },
  "vscode": {
   "interpreter": {
    "hash": "369f2c481f4da34e4445cda3fffd2e751bd1c4d706f27375911949ba6bb62e1c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
